// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import RankItem from "./rankItem";
import {GameVar} from "./GameVar";
import Hero from "../hero";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Rank extends cc.Component {

  private hero: Hero = null;
  @property(cc.Prefab)
  rankItemPrefab: cc.Prefab = null;

  @property(cc.Node)
  rankHide: cc.Node = null;

  @property(cc.Node)
  heroRankItem: cc.Node = null;

  rankData: Array<{ id: number, name: string, score: number }> = []
  rankItems: Array<RankItem> = []

  onLoad() {
    this.initRank();
    this.node.on(cc.Node.EventType.TOUCH_START, (e) => {
      e.stopPropagation()
      this.rankHide.active = true;
      this.node.active = false;
    })
  }

  setHero(hero: Hero) {
    this.hero = hero
    const heroRankItem = this.heroRankItem.getComponent(RankItem);
    heroRankItem.setName(this.hero.user.username);
    heroRankItem.setRank(1);
    heroRankItem.setScore(0);
    heroRankItem.setMe();
  }

  private initRank() {
    const layout = this.node.getChildByName('layout')
    for (let i = 0; i < 5; i++) {
      const rankItem = layout.children[i].getComponent(RankItem)
      this.rankItems.push(rankItem)
    }
  }

  addUser(...users: Array<{ uid: number, username: string }>) {
    users.forEach(value => {
      this.rankData.push({id: value.uid, name: value.username, score: 0})
    })
  }

  updateScore(uid: number, score: number) {
    if (GameVar.gameEnd) {
      return;
    }
    const item = this.rankData.find(value => value.id === uid)
    if (item != null) {
      item.score = score;
      this.updateRankItem();
    }
  }

  private updateMyRankItem() {
    const heroRankItem = this.heroRankItem.getComponent(RankItem);
    for (let i = 0; i < this.rankData.length; i++) {
      const data = this.rankData[i];
      if (data.id === this.hero.user.uid) {
        heroRankItem.setScore(data.score);
        heroRankItem.setRank(i + 1);
        break;
      }
    }

  }

  private updateRankItem() {
    this.rankData.sort((a, b) => {
      if (b.score != a.score) {
        return b.score - a.score;
      } else {
        return a.id - b.id
      }
    })
    const min = Math.min(5, this.rankData.length)
    for (let i = 0; i < min; i++) {
      const rankItem = this.rankItems[i];
      const rankData = this.rankData[i];
      rankItem.setName(rankData.name)
      rankItem.setScore(rankData.score)
      rankItem.setRank(i + 1)
      if (rankData.id === this.hero.user.uid) {
        rankItem.setMe();
      } else {
        rankItem.setOther();
      }
    }
    this.updateMyRankItem();
  }

  getRankById(heroId: number) {
    for (let i = 0; i < this.rankData.length; i++) {
      if (this.rankData[i].id === heroId) {
        return i + 1
      }
    }
  }

  reset() {
    this.rankData = []
    for (let i = 0; i < 5; i++) {
      const rankItem = this.rankItems[i];
      rankItem.reset()
    }
  }
}
